package core.command;

import java.util.ArrayList;

import core.MyCursor;
import core.Point2D;
import core.Point2DUtil;
import core.Stroke;

public class StrokesTransformation {
	
	private ArrayList<Stroke> strokes;
	private MyCursor cursor0;
	private MyCursor cursor1;
	private Point2D cursor0PreviousPosition;
	private Point2D cursor0CurrentPosition;
	private Point2D cursor1PreviousPosition;
	private Point2D cursor1CurrentPosition;
	private int cursorId;

	public StrokesTransformation(
			ArrayList<Stroke> strokes,
			int cursorId,
			MyCursor cursor0,
			MyCursor cursor1,
			Point2D cursor0CurrentPosition,
			Point2D cursor0PreviousPosition,
			Point2D cursor1CurrentPosition,
			Point2D cursor1PreviousPosition
	) {	
		this.strokes = strokes;
		this.cursorId = cursorId;
		this.cursor0 = cursor0;
		this.cursor1 = cursor1;
		this.cursor0PreviousPosition = cursor0PreviousPosition;
		this.cursor0CurrentPosition = cursor0CurrentPosition;		this.cursor1PreviousPosition = cursor1PreviousPosition;
		this.cursor1CurrentPosition = cursor1CurrentPosition;
	}
	
	public void execute() {
		for ( Stroke s : this.strokes ) {
			Point2DUtil.transformPointsBasedOnDisplacementOfTwoPoints(
				s.getPoints(),
				this.cursorId==cursor0.id ? this.cursor0PreviousPosition : this.cursor0CurrentPosition,
				this.cursorId==cursor1.id ? this.cursor1PreviousPosition : this.cursor1CurrentPosition,
				this.cursor0CurrentPosition,
				this.cursor1CurrentPosition
			);
			s.markBoundingRectangleDirty();
		}
	}

	public void undo() {
		for ( Stroke s : this.strokes ) {
			Point2DUtil.transformPointsBasedOnDisplacementOfTwoPoints(
				s.getPoints(),
				this.cursorId==cursor0.id ? this.cursor0CurrentPosition : this.cursor0PreviousPosition,
				this.cursorId==cursor1.id ? this.cursor1CurrentPosition : this.cursor1PreviousPosition,
				this.cursor0PreviousPosition,
				this.cursor1PreviousPosition
			);
			s.markBoundingRectangleDirty();
		}
	}
}
